%% MAR
mar_pre_t = mean(exp(va_pre_t(:,1:3)),2);
mar_post_t = mean(exp(va_post_t(:,1:3)),2);
mar_pre_ct = mean(exp(va_pre_ct(:,1:3)),2);
mar_post_ct = mean(exp(va_post_ct(:,1:3)),2);
mar_pre_c = mean(exp(va_pre_c(:,1:3)),2);
mar_post_c = mean(exp(va_post_c(:,1:3)),2);

m_mar_pre_t = mean(mar_pre_t);
m_mar_post_t = mean(mar_post_t);
m_mar_pre_ct = mean(mar_pre_ct);
m_mar_post_ct = mean(mar_post_ct);
m_mar_pre_c = mean(mar_pre_c);
m_mar_post_c = mean(mar_post_c);

se_mar_pre_t = std(mar_pre_t)/sqrt(8);
se_mar_post_t = std(mar_post_t)/sqrt(8);
se_mar_pre_ct = std(mar_pre_ct)/sqrt(8);
se_mar_post_ct = std(mar_post_ct)/sqrt(8);
se_mar_pre_c = std(mar_pre_c)/sqrt(8);
se_mar_post_c = std(mar_post_c)/sqrt(8);


m_mar_pre_t2 = mean(exp(far.t_pre));
m_mar_post_t2 = mean(exp(far.t_post));
m_mar_pre_ct2 = mean(exp(far.ct_pre));
m_mar_post_ct2 = mean(exp(far.ct_post));
m_mar_pre_c2 = mean(exp(far.c_pre));
m_mar_post_c2 = mean(exp(far.c_post));

se_mar_pre_t2 = std(exp(far.t_pre))/sqrt(8);
se_mar_post_t2 = std(exp(far.t_post))/sqrt(8);
se_mar_pre_ct2 = std(exp(far.ct_pre))/sqrt(8);
se_mar_post_ct2 = std(exp(far.ct_post))/sqrt(8);
se_mar_pre_c2 = std(exp(far.c_pre))/sqrt(8);
se_mar_post_c2 = std(exp(far.c_post))/sqrt(8);

% colors2 = colors*1.2;
% colors2(colors2>1) = 1;
colorsF2 = [0.6259 0.9788 1.0000; 0.5365 0.8471 0.5365; 0.6259 0.9788 1.0000; 0.5365 0.8471 0.5365; 0.6259 0.9788 1.0000; 0.5365 0.8471 0.5365];

figure('color',[1 1 1])
subplot(1,2,1)
% subplot(1,1,1)
ndata = [m_mar_pre_t  m_mar_post_t  m_mar_pre_ct  m_mar_post_ct m_mar_pre_c  m_mar_post_c];
ind_ndata = [mar_pre_t  mar_post_t  mar_pre_ct  mar_post_ct  mar_pre_c  mar_post_c];
se_ndata = [se_mar_pre_t se_mar_post_t se_mar_pre_ct se_mar_post_ct se_mar_pre_c se_mar_post_c];
% xaxisF = [1 2 4 5 7 8];

xaxisF = [1 1.4 4 4.4 7 7.4];

errorbar(xaxisF, ndata,se_ndata, 'ko','MarkerFaceColor',[1 1 1],'linewidth',1, 'CapSize',0, 'MarkerSize',1);


for i = 1:6
    
    hold on
    
    plot(xaxisF(i),ndata(:,i),'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,colorsF2(i,:),'MarkerSize',10);
end

% % errorbar(xaxisF,ndata,se_ndata,'ko','MarkerFaceColor',[1 1 1],'linewidth',1,'CapSize',0)
% % errorbar(xaxisF, ndata,se_ndata, 'ko','MarkerFaceColor',[1 1 1],'linewidth',1, 'CapSize',0, 'MarkerSize',1);

% % X = [1,2];
% % Y = [ndata(:,1),ndata(:,2)];
% % plot(X,Y,'.-k', 'linewidth',1)  
% % 
% % X = [4,5];
% % Y = [ndata(:,3),ndata(:,4)];
% % plot(X,Y,'.-k', 'linewidth',1)  
% % 
% % X = [7,8];
% % Y = [ndata(:,5),ndata(:,6)];
% % plot(X,Y,'.-k', 'linewidth',1) 

X = [1,4];
Y = [ndata(:,1),ndata(:,3)];
plot(X,Y,'.-k', 'linewidth',1)  

X = [4,7];
Y = [ndata(:,3),ndata(:,5)];
plot(X,Y,'.-k', 'linewidth',1)  


% X = [2,5];
X = [1.4,4.4];
Y = [ndata(:,2),ndata(:,4)];
plot(X,Y,'.-k', 'linewidth',1)  
% legend('pre','post')
% X = [5,8];
X = [4.4,7.4];
Y = [ndata(:,4),ndata(:,6)];
plot(X,Y,'.-k', 'linewidth',1) 


PP1 = plot (1, m_mar_pre_t,'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.6259 0.9788 1.0000],'MarkerSize',10)
% PP2 = plot (2, m_mar_post_t, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
PP2 = plot (1.4, m_mar_post_t, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
PP3 = plot (4, m_mar_pre_ct,'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.6259 0.9788 1.0000],'MarkerSize',10)
% PP4 = plot (5, m_mar_post_ct, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
PP4 = plot (4.4, m_mar_post_ct, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
PP5 = plot (7, m_mar_pre_c,'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.6259 0.9788 1.0000],'MarkerSize',10)
% PP6 = plot (8, m_mar_post_c, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
PP6 = plot (7.4, m_mar_post_c, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)


hold on
box off
ylabel('VA (MAR)');
% ylabel('Minimum Angle of Resoultion');
% set(gca,'xlim',[0 8.5],'ylim',[0 2],'xtick',[1.5 4.5 7.5], 'xticklabel',{'Alternating', 'Fixed', 'Control'}, 'fontsize',10,'linewidth',1)
% set(gca,'xlim',[0 8.5],'ylim',[1.2 1.55],'xtick',[1.5 4.5 7.5], 'xticklabel',{'Alternating', 'Fixed', 'Control'}, 'fontsize',10,'linewidth',1)
set(gca,'xlim',[0 8.5],'ylim',[1.2 1.55],'xtick',[1.2 4.2 7.2], 'xticklabel',{'Alternating', 'Fixed', 'Control'}, 'FontName', 'Arial','fontsize',10,'linewidth',1)
title('Near distance')
hold on

%% Far distance

% subplot(2,1,2)
subplot(1,2,2)
% xaxisF = [1 2 4 5 7 8];
xaxisF = [1 1.4 4 4.4 7 7.4];
fdata = [m_mar_pre_t2  m_mar_post_t2  m_mar_pre_ct2  m_mar_post_ct2 m_mar_pre_c2  m_mar_post_c2];
ind_fdata = [exp(far.t_pre)  exp(far.t_post)  exp(far.ct_pre)  exp(far.ct_post)  exp(far.c_pre)  exp(far.c_post)];
se_fdata = [se_mar_pre_t2 se_mar_post_t2 se_mar_pre_ct2 se_mar_post_ct2 se_mar_pre_c2 se_mar_post_c2];

errorbar(xaxisF, fdata,se_fdata, 'ko','MarkerFaceColor',[1 1 1],'linewidth',1, 'CapSize',0, 'MarkerSize',1);

for i = 1:6

    hold on
    
    plot(xaxisF(i),fdata(:,i),'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,colorsF2(i,:),'MarkerSize',10);

end

% % errorbar(xaxisF, fdata,se_fdata, 'ko','MarkerFaceColor',[1 1 1],'linewidth',1, 'CapSize',0, 'MarkerSize',1);


% X = [1,2];
% Y = [fdata(:,1),fdata(:,2)];
% plot(X,Y,'.-k', 'linewidth',1)  
% legend('pre','post')
% X = [4,5];
% Y = [fdata(:,3),fdata(:,4)];
% plot(X,Y,'.-k', 'linewidth',1)  
% legend('pre','post')
% X = [7,8];
% Y = [fdata(:,5),fdata(:,6)];
% plot(X,Y,'.-k', 'linewidth',1)  


X = [1,4];
Y = [fdata(:,1),fdata(:,3)];
plot(X,Y,'.-k', 'linewidth',1)

X = [4,7];
Y = [fdata(:,3),fdata(:,5)]; 
plot(X,Y,'.-k', 'linewidth',1)  
hold on

% X = [2,5];
X = [1.4,4.5];
Y = [fdata(:,2),fdata(:,4)]; 
plot(X,Y,'.-k', 'linewidth',1) 


% X = [5,8];
X = [4.4,7.4];
Y = [fdata(:,4),fdata(:,6)];
plot(X,Y,'.-k', 'linewidth',1) 


P1 = plot (1, m_mar_pre_t2,'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.6259 0.9788 1.0000],'MarkerSize',10)
% P2 = plot (2, m_mar_post_t2, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
P2 = plot (1.4, m_mar_post_t2, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
l = cell(1,2);
l{1}='Pre'; l{2}='Post'; 
[hLg, icons] = legend([P1,P2],l,'location','northwest');
legend boxoff
P3 = plot (4, m_mar_pre_ct2,'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.6259 0.9788 1.0000],'MarkerSize',10)
% P4 = plot (5, m_mar_post_ct2, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
P4 = plot (4.4, m_mar_post_ct2, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
P5 = plot (7, m_mar_pre_c2,'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.6259 0.9788 1.0000],'MarkerSize',10)
% P6 = plot (8, m_mar_post_c2, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)
P6 = plot (7.4, m_mar_post_c2, 'o', 'MarkerEdgeColor' ,[0 0 0],'MarkerFaceColor' ,[0.5365 0.8471 0.5365],'MarkerSize',10)


% % legend('pre','post')
% % legend boxoff

hold on
box off
ylabel('VA (MAR)');
% ylabel('Minimum Angle of Resoultion');
% % set(gca,'xlim',[0 8.5],'ylim',[0 1.4],'xtick',[1.5 4.5 7.5], 'xticklabel',{'Alternating', 'Fixed', 'Control'}, 'fontsize',10,'linewidth',1)
% set(gca,'xlim',[0 8.5],'ylim',[0.9 1.15],'xtick',[1.5 4.5 7.5], 'xticklabel',{'Alternating', 'Fixed', 'Control'}, 'fontsize',10,'linewidth',1)
set(gca,'xlim',[0 8.5],'ylim',[0.9 1.15],'xtick',[1.2 4.2 7.2], 'xticklabel',{'Alternating', 'Fixed', 'Control'}, 'FontName', 'Arial', 'fontsize',10,'linewidth',1)
title('Far distance')

% set(gcf,'units','centimeters','position',[0 0 19 9])
 set(gcf,'units','centimeters','position',[0 0 24 9])
 
 
 % Significance test: (*p < 0.05; **p < 0.01; ***p < 0.001).
% near
 [h,p,ci,stats] = ttest(mar_pre_t, mar_post_t); % p = 0.0458,t7 = 2.4243 not significant (mar: p =0.0606, t7 = 2.2346) 
 [h,p,ci,stats] = ttest(mar_pre_ct, mar_post_ct); % *p = 0.0140, t7 = 3.251 (mar: *p =  0.0188, t7 = 3.0410) 
 [h,p,ci,stats] = ttest(mar_pre_c, mar_post_c); % p = 0.6770, t7 = -0.4345 (mar: p =   0.7819, t7 = -0.2877) 
 
 
% far
 [h,p,ci,stats] = ttest(exp(far.t_pre), exp(far.t_post)); % *p = 0.0211, t7 = 2.959  (mar: *p =0.0239, t7 = 2.8719) 
 [h,p,ci,stats] = ttest(exp(far.ct_pre), exp(far.ct_post));% **p =  0.0070, t7 = 3.766 (mar: **p = 0.0072, t7 = 3.7495) 
 [h,p,ci,stats] = ttest(exp(far.c_pre), exp(far.c_post)); % p = 0.7627, t7 = 0.3140 (mar: p = 0.7436, t7 = 0.3404) 
 
 
 